<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/10
 * Time: 11:22
 */

namespace app\api\controller;


use think\Db;

class Beautydiary extends Base
{
    public $user_id;
    public $create_time;
    public $page;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 美丽日记分类
     * @return \think\response\Json
     */
    public function diaryCate(){
        $cate = Db::table('up_beauty_diary_cate')
            ->where('bdc_status', 1)
            ->field('bdc_id, bdc_name, bdc_is_default')
            ->select();
        if($cate){
            return json(['status' => 200, 'msg' => $cate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 发布美丽日记
     * @return \think\response\Json
     * bd_bdc_id      分类DI，不选就传空
     * bd_word        文字
     * bd_images      图片
     * bd_video       视频
     * bd_is_show     1  公开   2 隐藏
     */
    public function diaryAdd(){
        $bd_bdc_id = input('bd_bdc_id');
        $bd_word = input('bd_word');
        $bd_images = input('bd_images');
        $bd_video = input('bd_video');
        $bd_is_show = input('bd_is_show');
        $return = $this->sensitiveWord($bd_word);
        if($return['status'] == 2) return json(['status' => 0, 'msg' => '“' . $return['msg'] . '”为敏感词，请修改']);
        
        $insert = [
            'bd_from' => 1,
            'bd_user_id' => $this->user_id,
            'bd_admin_id' => 0,
            'bd_status' => 1,
            'bd_word' => $bd_word,
            'bd_images' => $bd_images,
            'bd_video' => $bd_video,
            'bd_is_show' => $bd_is_show,
            'bd_time' => $this->create_time
        ];
        if($bd_bdc_id){
            $insert['bd_bdc_id'] = $bd_bdc_id;
        }else{
            $bd_cate = Db::table('up_beauty_diary_cate')
                ->where(['bdc_status' => 1, 'bdc_is_default' => 2])
                ->value('bdc_id');
            if($bd_cate){
                $insert['bd_bdc_id'] = $bd_cate;
            }else{
                return json(['code' => 100, 'msg' => '请选择分类']);
            }
        }
        
        $res = Db::table('up_beauty_diary')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 美丽日记列表
     * @return \think\response\Json
     * list_type     1 推荐   2 关注   3 全部
     */
    public function diaryList(){
        $list_type = input('list_type'); // 1 推荐   2 关注   3 全部
        $unlike = Db::table('up_user_unlike')
            ->where(['u_user_id' => $this->user_id, 'u_unlike_user_id' => ['NEQ', 0]])
            ->column('u_unlike_user_id');

        $where['bd_status'] = 1;
        $where['bd_is_show'] = 1;
        if($unlike){
            $where['bd_user_id'] = ['NOTIN', $unlike];
        }

        $list = Db::table('up_beauty_diary')
            ->join('up_beauty_diary_cate', 'bdc_id = bd_bdc_id', 'LEFT')
            ->field('bd_comment_number, bd_id, bd_from, bd_user_id, bdc_name, bd_dz_number, bd_word, 
            bd_images, bd_video, bd_time, bd_is_show')
            ->order('bd_time desc')
            ->page($this->page, 10)
            ->where($where);
//            ->where(function ($query){
//                $query->where('bd_is_show', 1)
//                    ->whereOr(function ($query1){
//                        $query1->where(['bd_is_show' => 2, 'bd_user_id' => $this->user_id]);
//                    });
//            });

        if($list_type == 1){
            $where1['bd_is_recommend'] = 2;
            $list = $list->where($where1)->select();
        }elseif($list_type == 2){
            $my_attent = Db::table('up_user_attent')->where('attent_my_id', $this->user_id)->column('attent_other_user_id');
            if($my_attent){
                $where1['bd_user_id'] = ['IN', $my_attent];
                $list = $list->where($where1)->select();
            }else{
                $list  = [];
            }
        }else{
            $list = $list->select();
        }
        if($list){
            foreach($list as $k => $v){
                // 用户
                if($v['bd_from'] == 1){
                    $user = Db::table('up_u_user')
                        ->where('user_id', $v['bd_user_id'])
                        ->field('user_username, user_image')
                        ->find();
                }else{
                    $user = ['user_username' => 'admin', 'user_image' => ''];
                }
                $list[$k]['user_username'] = $user['user_username'];
                $list[$k]['user_image'] = $user['user_image'];
                
                // 是否已关注
                $list[$k]['is_attent'] = $this->isAttent($v['bd_user_id']);
                
                // 图片
                $list[$k]['bd_images'] = $this->explodeImages($v['bd_images']);
            }
        }
        return json(['status' => 200, 'msg' => $list]);
    }

    /**
     * 隐藏/公开
     * bd_id     日记ID
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function showOrPublic(){
        $bd_id = input('bd_id');
        $diary = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->field('bd_is_show')->find();
        $newShow = $diary['bd_is_show'] == 1 ? 2 : 1;
        $res = Db::table('up_beauty_diary')->update(['bd_id' => $bd_id, 'bd_is_show' => $newShow]);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 关注/取消关注
     * @return \think\response\Json
     * other_user_id    其他用户ID
     */
    public function userAttent(){
        $other_user_id = input('other_user_id');
        $isAttent = Db::table('up_user_attent')
            ->where('attent_my_id', $this->user_id)
            ->where('attent_other_user_id', $other_user_id)
            ->field('attent_id')
            ->find();
        if($isAttent){ // 已关注，取消关注
            $res = Db::table('up_user_attent')->delete($isAttent['attent_id']);
            $attent = 2;
        }else{
            $res = Db::table('up_user_attent')
                ->insert([
                    'attent_my_id' => $this->user_id,
                    'attent_other_user_id' => $other_user_id,
                    'attent_time' => $this->create_time
                ]);
            $attent = 1;
        }
        if($res){
            return json(['status' => 200, 'msg' => $attent]);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 举报类型
     * @return \think\response\Json
     */
    public function diaryReportType(){
        $type = Db::table('up_beauty_diary_report_type')
            ->where('bdrt_status', 1)
            ->field('bdrt_id, bdrt_name')
            ->select();
        if($type){
            return json(['status' => 200, 'msg' => $type]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 美丽日记举报
     * @return \think\response\Json
     * bd_id       美丽日记ID
     * bdrt_id     举报类型ID
     * bdr_content    举报内容
     */
    public function diaryReport(){
        $bd_id = input('bd_id');
        $bdrt_id = input('bdrt_id');
        $bdr_content = input('bdr_content');
        
        $diary = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->field('bd_user_id')->find();
        if($diary['bd_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => '不可以举报自己的美丽日记']);
        $is_report = Db::table('up_beauty_diary_report')
            ->where(['bdr_user_id' => $this->user_id, 'bdr_bd_id' => $bd_id, 'bdr_type' => 1])
            ->find();
        if($is_report) return json(['status' => 0, 'msg' => '您已举报过该日记']);
        
        $insert = [
            'bdr_user_id' => $this->user_id,
            'bdr_other_user_id' => $diary['bd_user_id'],
            'bdr_bd_id' => $bd_id,
            'bdr_bdrt_id' => $bdrt_id,
            'bdr_type' => 1,
            'bdr_content' => $bdr_content,
            'bdr_time' => $this->create_time
        ];
        $res = Db::table('up_beauty_diary_report')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '举报成功']);
        }else{
            return json(['status' => 0, 'msg' => '举报失败']);
        }
    }
    
    /**
     * 美丽日记详情
     * @return \think\response\Json
     * bd_id      美丽日记ID
     */
    public function diaryDetail(){
        header("Access-Control-Allow-Origin: *");
        $bd_id = input('bd_id');
        $detail = Db::table('up_beauty_diary')
            ->join('up_beauty_diary_cate', 'bdc_id = bd_bdc_id', 'LEFT')
            ->where('bd_id', $bd_id)
            ->field('bd_comment_number, bd_id, bd_from, bd_user_id, bdc_name, bd_dz_number, bd_word, 
            bd_images, bd_video, bd_time')
            ->find();
        
        // 图片
        $detail['bd_images'] = $this->explodeImages($detail['bd_images']);
        
        // 用户
        if($detail['bd_from'] == 1){
            $user = Db::table('up_u_user')
                ->where('user_id', $detail['bd_user_id'])
                ->field('user_username, user_image')
                ->find();
        }else{
            $user = ['user_username' => 'admin', 'user_image' => ''];
        }
        $detail['user_username'] = $user['user_username'];
        $detail['user_image'] = $user['user_image'];

        if($this->user_id == 0){
            $detail['is_attent'] = 2;
            $detail['is_dz'] = 2;
        }else{
            // 是否已关注
            $detail['is_attent'] = $this->isAttent($detail['bd_user_id']);

            // 是否已点赞
            $detail['is_dz'] = $this->isDz($bd_id);
        }

        
        // 评论
        $comment = Db::table('up_beauty_diary_comment')
            ->where('bdc_bd_id', $detail['bd_id'])
            ->where('bdc_status', 1)
            ->field('bdc_id, bdc_user_id, bdc_admin_id, bdc_content, bdc_from, bdc_time')
            ->page($this->page, 10)
            ->order('bdc_time desc')
            ->select();
        if($comment){
            foreach($comment as $k => $v){
                // 评论用户
                if($v['bdc_from'] == 1){
                    $user_comment = Db::table('up_u_user')
                        ->where('user_id', $v['bdc_user_id'])
                        ->field('user_username, user_image')
                        ->find();
                }else{
                    $user_comment = ['user_username' => 'admin', 'user_image' => ''];
                }
                $comment[$k]['user_username'] = $user_comment['user_username'];
                $comment[$k]['user_image'] = $user_comment['user_image'];
                
                // 回复
                $reply = Db::table('up_beauty_diary_reply')
                    ->where('reply_comment_id', $v['bdc_id'])
                    ->where('reply_status', 1)
                    ->field('reply_id, reply_type, reply_user_id, reply_bd_id, reply_comment_id, reply_reply_id, 
                    reply_content, reply_from, reply_time')
//                    ->order('reply_time desc')
                    ->select();
                if($reply){
                    $reply_number = count($reply);
                    foreach($reply as $kk => $vv){
                        // 回复用户
                        if($vv['reply_from'] == 1){
                            $user_reply = Db::table('up_u_user')
                                ->where('user_id', $vv['reply_user_id'])
                                ->field('user_username')
                                ->find();
                        }else{
                            $user_reply = ['user_username' => 'admin'];
                        }
                        $reply[$kk]['user_username'] = $user_reply['user_username'];
                        
                        // 如果回复的是回复，需要知道回复的是谁
                        if($vv['reply_type'] == 2){
                            $to_user = Db::table('up_beauty_diary_reply')
                                ->where('reply_id', $vv['reply_reply_id'])
                                ->field('reply_user_id, reply_from')
                                ->find();
                            // 回复的是app用户
                            if($to_user['reply_from'] == 1){
                                $reply_repley_user = Db::table('up_u_user')
                                    ->where('user_id', $to_user['reply_user_id'])
                                    ->field('user_id, user_username')
                                    ->find();
                            }else{
                                $reply_repley_user = ['user_username' => 'admin', 'user_id' => ''];
                            }
                            $reply[$kk]['to_username'] = $reply_repley_user['user_username'];
                            $reply[$kk]['to_user_id'] = $reply_repley_user['user_id'];
                        }
                    }
                }else{
                    $reply_number = 0;
                    $reply = [];
                }
                $comment[$k]['reply_number'] = $reply_number;
                $comment[$k]['reply'] = $reply;
            }
            $detail['comment'] = $comment;
        }else{
            $detail['comment'] = [];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 评论美丽日记
     * @return \think\response\Json
     * bd_id      日记ID
     * bdc_content    评论内容
     *
     */
    public function commentDiary(){
        $bd_id = input('bd_id');
        $bdc_content = input('bdc_content');
        $insert = [
            'bdc_user_id' => $this->user_id,
            'bdc_admin_id' => 0,
            'bdc_bd_id' => $bd_id,
            'bdc_content' => $bdc_content,
            'bdc_from' => 1,
            'bdc_time' => $this->create_time
        ];
        $diary = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->field('bd_user_id, bd_from, bd_time')->find();
        Db::startTrans();
        try{
            // 入评论表
            $bdc_id = Db::table('up_beauty_diary_comment')->insertGetId($insert);
            
            // 入消息表
            if($diary['bd_from'] == 1){
                $notice = [
                    'notice_title' => '您于' . $diary['bd_time'] . '发布的美丽日记被评论了！点击此消息立即查看吧！',
                    'notice_range' => 2,
                    'notice_type' => 2,
                    'notice_bd_id' => $bd_id,
                    'notice_comment_id' => $bdc_id,
                    'notice_reply_id' => 0,
                    'notice_reply_reply_id' => 0,
                    'notice_user' => ',' . $diary['bd_user_id'] . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time
                ];
                Db::table('up_notice')->insert($notice);
            }
            
            // 评论数量+1
            Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setInc('bd_comment_number');
            
            Db::commit();
            return json(['status' => 200, 'msg' => '评论成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 回复评论
     * @return \think\response\Json
     * comment_id      评论ID
     * reply_content    回复内容
     */
    public function replyComment(){
        $comment_id = input('comment_id');
        $reply_content = input('reply_content');
        $comment = Db::table('up_beauty_diary_comment')
            ->where('bdc_id', $comment_id)
            ->field('bdc_bd_id, bdc_user_id, bdc_from, bdc_time')
            ->find();
        
        $insert = [
            'reply_type' => 1,
            'reply_user_id' => $this->user_id,
            'reply_admin_id' => 0,
            'reply_bd_id' => $comment['bdc_bd_id'],
            'reply_comment_id' => $comment_id,
            'reply_reply_id' => 0,
            'reply_content' => $reply_content,
            'reply_from' => 1,
            'reply_time' => $this->create_time
        ];
        
        Db::startTrans();
        try{
            // 入回复表
            $reply_id = Db::table('up_beauty_diary_reply')->insertGetId($insert);
            
            // 如果是app用户发布的评论，则入消息表
            if($comment['bdc_from'] == 1){
                $notice = [
                    'notice_title' => '您于' . $comment['bdc_time'] . '发布的评论被回复了！点击此消息立即查看吧！',
                    'notice_range' => 2,
                    'notice_type' => 3,
                    'notice_bd_id' => $comment['bdc_bd_id'],
                    'notice_comment_id' => $comment_id,
                    'notice_reply_id' => $reply_id,
                    'notice_reply_reply_id' => 0,
                    'notice_user' => ',' . $comment['bdc_user_id'] . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time
                ];
                Db::table('up_notice')->insert($notice);
            }
            
            Db::commit();
            return json(['status' => 200, 'msg' => '回复成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 回复回复
     * @return \think\response\Json
     * reply_id      被回复的回复ID
     * reply_content    回复内容
     */
    public function replyReply(){
        $reply_id = input('reply_id');
        $reply_content = input('reply_content');
        $reply = Db::table('up_beauty_diary_reply')
            ->where('reply_id', $reply_id)
            ->field('reply_user_id, reply_bd_id, reply_comment_id, reply_from, reply_time')
            ->find();
        
        $insert = [
            'reply_type' => 2,
            'reply_user_id' => $this->user_id,
            'reply_admin_id' => 0,
            'reply_bd_id' => $reply['reply_bd_id'],
            'reply_comment_id' => $reply['reply_comment_id'],
            'reply_reply_id' => $reply_id,
            'reply_content' => $reply_content,
            'reply_from' => 1,
            'reply_time' => $this->create_time
        ];
        
        Db::startTrans();
        try{
            // 插入回复表
            $reply_reply_id = Db::table('up_beauty_diary_reply')->insertGetId($insert);
            
            // 如果是app用户发布的回复，则入消息表
            if($reply['reply_from'] == 1){
                $notice = [
                    'notice_title' => '您于' . $reply['reply_time'] . '发布的回复被回复了！点击此消息立即查看吧！',
                    'notice_range' => 2,
                    'notice_type' => 4,
                    'notice_bd_id' => $reply['reply_bd_id'],
                    'notice_comment_id' => $reply['reply_comment_id'],
                    'notice_reply_id' => $reply_id,
                    'notice_reply_reply_id' => $reply_reply_id,
                    'notice_user' => ',' . $reply['reply_user_id'] . ',',
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time
                ];
                Db::table('up_notice')->insert($notice);
            }
            
            Db::commit();
            return json(['status' => 200, 'msg' => '回复成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 点赞/取消点赞
     * @return \think\response\Json
     * bd_id    美丽日记ID
     */
    public function dz(){
        $bd_id = input('bd_id');
        $dz = $this->isDz($bd_id);
        
        Db::startTrans();
        try{
            if($dz == 1){
                // 删除点赞
                Db::table('up_beauty_diary_dz')->where(['bddz_bd_id' => $bd_id, 'bddz_user_id' => $this->user_id])->delete();
                
                // 减少点赞数量
                Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setDec('bd_dz_number');
                $is_dz = 2;
            }else{
                // 插入点赞表
                Db::table('up_beauty_diary_dz')
                    ->insert([
                        'bddz_bd_id' => $bd_id,
                        'bddz_user_id' => $this->user_id,
                        'bddz_time' => $this->create_time
                    ]);
                
                // 增加点赞数量
                Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setInc('bd_dz_number');
                $is_dz = 1;
            }
            
            Db::commit();
            return json(['status' => 200, 'msg' => $is_dz]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
        
    }
    
    /**
     * 举报评论
     * @return \think\response\Json
     * bdc_id      评论ID
     * bdrt_id     举报类型
     * bdr_content   举报内容
     */
    public function reportComment(){
        $bdc_id = input('bdc_id');
        $bdrt_id = input('bdrt_id');
        $bdr_content = input('bdr_content');
        
        $comment = Db::table('up_beauty_diary_comment')->where('bdc_id', $bdc_id)->field('bdc_user_id')->find();
        if($comment['bdc_user_id'] == $this->user_id) return json(['status' => 0, 'msg' => '不可以举报自己的评论']);
        
        $is_report = Db::table('up_beauty_diary_report')
            ->where(['bdr_user_id' => $this->user_id, 'bdr_comment_id' => $bdc_id, 'bdr_type' => 2])
            ->find();
        if($is_report) return json(['status' => 0, 'msg' => '您已举报过该评论']);
    
        $insert = [
            'bdr_user_id' => $this->user_id,
            'bdr_other_user_id' => $comment['bdc_user_id'],
            'bdr_comment_id' => $bdc_id,
            'bdr_bdrt_id' => $bdrt_id,
            'bdr_type' => 2,
            'bdr_content' => $bdr_content,
            'bdr_time' => $this->create_time
        ];
        $res = Db::table('up_beauty_diary_report')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '举报成功']);
        }else{
            return json(['status' => 0, 'msg' => '举报失败']);
        }
    }
    
    /**
     * 用户主页基本信息
     * @return \think\response\Json
     * other_user_id     他人ID,如果是自己的主页，则可不传，可传空
     */
    public function userMain(){
        $other_user_id = input('other_user_id');
        if(isset($other_user_id) && $other_user_id != ''){
            $uid = $other_user_id;
        }else{
            $uid = $this->user_id;
        }
        $user = Db::table('up_u_user')->where('user_id', $uid)->field('user_username, user_image')->find();
        // 关注数
        $user['attent_number'] = $this->attent($uid, 2);
        // 粉丝数
        $user['fan_number'] = $this->fan($uid, 2);
        
        // 他人主页，需要给出是否关注、是否已经不感兴趣
        if($uid != $this->user_id){
            // 是否拉黑
            $is_unlike = $this->isUnlike($uid);
            $user['is_unlike'] = $is_unlike['status'];
            // 是否关注
            $user['is_attent'] = $this->isAttent($uid);
        }
        
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 删除美丽日记
     * @return \think\response\Json
     * bd_id       美丽日记ID
     */
    public function delDiary(){
        $bd_id = input('bd_id');
        $diary = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->field('bd_user_id')->find();
        if($diary['bd_user_id'] != $this->user_id) return json(['status' => 0, 'msg' => '只可以删除自己发布的美丽日记']);
        $res = Db::table('up_beauty_diary')->where('bd_id', $bd_id)->setField('bd_status', 3);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 用户美丽日记列表
     * @return \think\response\Json
     * other_user_id     他人ID，本人主页可不传或传空
     */
    public function userMainDiary(){
        $other_user_id = input('other_user_id');
        if(isset($other_user_id) && $other_user_id != ''){
            $uid = $other_user_id;
        }else{
            $uid = $this->user_id;
        }
        $diary = Db::table('up_beauty_diary')
            ->join('up_u_user', 'user_id = bd_user_id', 'LEFT')
            ->join('up_beauty_diary_cate', 'bdc_id = bd_bdc_id', 'LEFT')
            ->where(['bd_user_id' => $uid, 'bd_status' => 1])
            ->field('bd_id, bdc_name, bd_dz_number, bd_comment_number, bd_word, bd_images, bd_video, 
            bd_time, user_id, user_username, user_image, bd_is_show')
            ->order('bd_time desc')
            ->page($this->page, 10)
            ->select();
        if($diary){
            foreach($diary as $k => $v){
                // 图片
                $diary[$k]['bd_images'] = $this->explodeImages($v['bd_images']);
                // 是否点赞
                $diary[$k]['is_dz'] = $this->isDz($v['bd_id']);
            }
            return json(['status' => 200, 'msg' => $diary]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 拉黑/取消拉黑
     * @return \think\response\Json
     * other_user_id    他人ID
     */
    public function unlike(){
        $other_user_id = input('other_user_id');
        $is_unlike = $this->isUnlike($other_user_id);
        if($is_unlike['status'] == 1){ // 已拉黑
            $res = Db::table('up_user_unlike')->delete($is_unlike['data']['u_id']);
            $unlike = 2;
        }else{
            $res = Db::table('up_user_unlike')
                ->insert(['u_user_id' => $this->user_id, 'u_unlike_user_id' => $other_user_id]);
            $unlike = 1;
        }
        
        if($res){
            return json(['status' => 200, 'msg' => $unlike]);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 我的关注列表
     * @return \think\response\Json
     */
    public function attentList(){
        $attent = $this->attent($this->user_id);
        if($attent){
            return json(['status' => 200, 'msg' => $attent]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的粉丝列表
     * @return \think\response\Json
     */
    public function fanList(){
        $fan = $this->fan($this->user_id);
        if($fan){
            foreach($fan as $k => $v){
                // 我是否关注了这个粉丝
                $fan[$k]['is_attent'] = $this->isAttent($v['user_id']);
            }
            return json(['status' => 200, 'msg' => $fan]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的评论
     * @return \think\response\Json
     */
    public function myComment(){
        // 我评论过的美丽日记
        $bd_ids = Db::table('up_beauty_diary_comment')
            ->distinct('bdc_bd_id')
            ->where(['bdc_user_id' => $this->user_id, 'bdc_status' => 1])
            ->column('bdc_bd_id');
        if($bd_ids){
            $data = Db::table('up_beauty_diary')
                ->where('bd_id', 'IN', $bd_ids)
                ->where('bd_status', 1)
                ->field('bd_id, bd_from, bd_user_id, bd_word, bd_images, bd_video, bd_time')
                ->order('bd_time desc')
                ->page($this->page, 10)
                ->select();
            if($data){
                foreach($data as $k => $v){
                    // 图片
                    $data[$k]['bd_images'] = $this->explodeImages($v['bd_images']);
                    
                    // 用户
                    if($v['bd_from'] == 1){
                        $user = Db::table('up_u_user')
                            ->where('user_id', $v['bd_user_id'])
                            ->field('user_username, user_image')
                            ->find();
                    }else{
                        $user = ['user_username' => 'admin', 'user_image' => ''];
                    }
                    $data[$k]['user_username'] = $user['user_username'];
                    $data[$k]['user_image'] = $user['user_image'];
                    
                    // 评论
                    $comment = Db::table('up_beauty_diary_comment')
                        ->join('up_u_user', 'bdc_user_id = user_id', 'LEFT')
                        ->where(['bdc_user_id' => $this->user_id, 'bdc_bd_id' => $v['bd_id'], 'bdc_status' => 1])
                        ->field('bdc_content, bdc_time, user_username, user_image')
                        ->order('bdc_time asc')
                        ->select();
                    $data[$k]['comment_number'] = count($comment);
                    $data[$k]['comment'] = $comment;
                }
                return json(['status' => 200, 'msg' => $data]);
            }else{
                return json(['status' => 200, 'msg' => []]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的回复
     * @return \think\response\Json
     */
    public function myReply(){
        // 我回复的评论ID
        $comment_ids = Db::table('up_beauty_diary_reply')
            ->where(['reply_user_id' => $this->user_id, 'reply_status' => 1])
            ->distinct('reply_comment_id')->column('reply_comment_id');
        
        // 我回复日记ID
        $bd_ids = Db::table('up_beauty_diary_reply')
            ->where(['reply_user_id' => $this->user_id, 'reply_status' => 1])
            ->distinct('reply_bd_id')->column('reply_bd_id');
        
        // 日记列表
        if($bd_ids){
            $diary = Db::table('up_beauty_diary')
                ->where(['bd_id' => ['IN', $bd_ids], 'bd_status' => 1])
                ->field('bd_id, bd_from, bd_user_id, bd_word, bd_images, bd_video, bd_time')
                ->order('bd_time desc')
                ->page($this->page, 10)
                ->select();
            foreach($diary as $k => $v){
                // 图片
                $diary[$k]['bd_images'] = $this->explodeImages($v['bd_images']);
    
                // 用户
                if($v['bd_from'] == 1){
                    $user = Db::table('up_u_user')
                        ->where('user_id', $v['bd_user_id'])
                        ->field('user_username, user_image')
                        ->find();
                }else{
                    $user = ['user_username' => 'admin', 'user_image' => ''];
                }
                $diary[$k]['user_username'] = $user['user_username'];
                $diary[$k]['user_image'] = $user['user_image'];
                
                // 评论
                $comment = Db::table('up_beauty_diary_comment')
                    ->where(['bdc_bd_id' => $v['bd_id'], 'bdc_id' => ['IN', $comment_ids], 'bdc_status' => 1])
                    ->field('bdc_id, bdc_user_id, bdc_content, bdc_from, bdc_time')
                    ->select();
                foreach($comment as $kk => $vv){
                    // 用户
                    if($vv['bdc_from'] == 1){
                        $user = Db::table('up_u_user')
                            ->where('user_id', $vv['bdc_user_id'])
                            ->field('user_username, user_image')
                            ->find();
                    }else{
                        $user = ['user_username' => 'admin', 'user_image' => ''];
                    }
                    $comment[$kk]['user_username'] = $user['user_username'];
                    $comment[$kk]['user_image'] = $user['user_image'];
                    
                    // 回复
                    $reply = Db::table('up_beauty_diary_reply')
                        ->join('up_u_user', 'user_id = reply_user_id', 'LEFT')
                        ->where(['reply_status' => 1, 'reply_comment_id' => $vv['bdc_id'], 'reply_user_id' => $this->user_id])
                        ->field('user_username, reply_content')
                        ->select();
                    $comment[$kk]['reply'] = $reply;
                }
                $diary[$k]['comment'] = $comment;
            }
            return json(['status' => 200, 'msg' => $diary]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 未读消息数量
     * @return \think\response\Json
     */
    public function unreadNumber(){
        $num = $this->getNotice(2);
        return json(['status' => 200, 'msg' => $num]);
    }
    
    /**
     * 系统消息列表
     * @return \think\response\Json
     * page    页码
     */
    public function noticeList(){
        $list = $this->getNotice();
        if($list){
            foreach($list as $k => $v){
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $list[$k]['is_read'] = 1; // 已读
                }else{
                    $list[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 标记已读
     * @return \think\response\Json
     * notice_id     消息ID
     */
    public function readNotice(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_read_user')
            ->find();
        if(strpos($notice['notice_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $notice['notice_read_user'] . $this->user_id . ',';
            Db::table('up_notice')->where('notice_id', $notice_id)->setField('notice_read_user', $read_user);
        }
        return json(['status' => 200, 'msg' => '']);
    }
    
    /**
     * 清空消息
     * @return \think\response\Json
     */
    public function delAllNotice(){
        $notice = $this->getNotice();
        if($notice){
            Db::startTrans();
            try{
                foreach($notice as $k => $v){
                    $del_user = $v['notice_del_user'] . $this->user_id . ',';
                    Db::table('up_notice')->where('notice_id', $v['notice_id'])->setField('notice_del_user', $del_user);
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '清空消息成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        }else{
            return json(['status' => 200, 'msg' => '没有需要清空的消息']);
        }
    }
    
    // *********************************   公共方法
    /**
     * 是否关注了某个用户
     * @param $other_user_id    其他用户ID
     * @return int
     */
    private function isAttent($other_user_id){
        $is_attent = Db::table('up_user_attent')
            ->where(['attent_my_id' => $this->user_id, 'attent_other_user_id' => $other_user_id])
            ->find();
        if($is_attent){
            return 1; // 已关注
        }else{
            return 2; // 未关注
        }
    }
    
    /**
     * 用户是否点赞了某个美丽日记
     * @param $bd_id     美丽日记ID
     * @return int
     */
    private function isDz($bd_id){
        $is_dz = Db::table('up_beauty_diary_dz')
            ->where(['bddz_bd_id' => $bd_id, 'bddz_user_id' => $this->user_id])
            ->find();
        if($is_dz){
            return  1; // 已点赞
        }else{
            return 2; // 未点赞
        }
    }
    
    /**
     * 将字符串图片地址转换数组
     * @param $images     图片
     * @return array
     */
    public function explodeImages($images){
        if($images){
             $images_arr =  explode(',', $images);
        }else{
            $images_arr =  [];
        }
        return $images_arr;
    }
    
    /**
     * 我的粉丝
     * @param $uid         我的ID
     * @param int $type    1 列表    2 统计数量
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function fan($uid, $type = 1){
        $data = Db::table('up_user_attent')
            ->join('up_u_user', 'attent_my_id = user_id', 'LEFT')
            ->where('attent_other_user_id', $uid);
        if($type == 1){ // 列表
            $data = $data->field('user_id, user_username, user_image')->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 我关注的人
     * @param $uid       我的ID
     * @param int $type  1 列表    2 统计数量
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    private function attent($uid, $type = 1){
        $data = Db::table('up_user_attent')
            ->join('up_u_user', 'attent_other_user_id = user_id', 'LEFT')
            ->where('attent_my_id', $uid);
        if($type == 1){ // 列表
            $data = $data->field('user_id, user_username, user_image')->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 是否拉黑了某个用户
     * @param $other_user_id    他人ID
     * @return int
     */
    private function isUnlike($other_user_id){
        $is_unlike = Db::table('up_user_unlike')
            ->where(['u_user_id' => $this->user_id, 'u_unlike_user_id' => $other_user_id])
            ->field('u_id')
            ->find();
        if($is_unlike){
            return ['status' => 1, 'data' => $is_unlike];
        }else{
            return ['status' => 2, 'msg' => []];
        }
    }
    
    /**
     * 获取系统消息
     * @param int $type   1 列表数据    2 未读消息统计
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    private function getNotice($type = 1){
        $where['notice_type'] = ['IN', '2,3,4'];
        $where['notice_user'] = [['like', '%,' . $this->user_id . ',%'], ['=', -1], 'or'];
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($type == 2){
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $query = Db::table('up_notice')->where($where);
        if($type == 1){
            $data = $query->order('notice_time desc')
                ->field('notice_id, notice_title, notice_bd_id, notice_read_user, notice_time, notice_del_user')
                ->page($this->page, 10)
                ->select();
        }else{
            $data = $query->count();
        }
        return $data;
    }
}